package com.estylesoft.pshevch.fw;

import com.estylesoft.pshevch.util.Browser;
import com.estylesoft.pshevch.util.PropertyLoader;
import com.estylesoft.pshevch.webdriver.WebDriverFactory;
import org.openqa.selenium.WebDriver;

import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * User: pshevchenko
 * Date: 08.04.13
 * Time: 10:13
 * To change this template use File | Settings | File Templates.
 */
public class ApplicationManager {
    private static final String SCREENSHOT_FOLDER = "target/screenshots/";
    private static final String SCREENSHOT_FORMAT = ".png";
    private static final int TIME_OUT = 30;

    private static WebDriver driver;
    private static String gridHubUrl;
    private static String baseUrl;
    private static Browser browser;

    private StringBuffer verificationErrors = new StringBuffer();
    private int tempTimeouts;

    private MovieHelper movieHelper;
    private NavigationHelper navigationHelper;
    private UserHelper userHelper;

    public ApplicationManager(){
        String username = PropertyLoader.loadProperty("user.username");
        String password = PropertyLoader.loadProperty("user.password");

        baseUrl = PropertyLoader.loadProperty("site.url");
        gridHubUrl = PropertyLoader.loadProperty("grid2.hub");

        browser = new Browser();
        browser.setName(PropertyLoader.loadProperty("browser.name"));
        browser.setVersion(PropertyLoader.loadProperty("browser.version"));
        browser.setPlatform(PropertyLoader.loadProperty("browser.platform"));

        driver = WebDriverFactory.getInstance(ApplicationManager.gridHubUrl, ApplicationManager.browser, username,
                password);
        resetTimeOuts();

        navigationHelper = new NavigationHelper(this);
        userHelper = new UserHelper(this);
        movieHelper = new MovieHelper(this);
    }

    private void resetTimeOuts(){
        driver.manage().timeouts().implicitlyWait(TIME_OUT, TimeUnit.SECONDS);
    }

    protected void changeTimeouts(int timeOut) {
        driver.manage().timeouts().implicitlyWait(timeOut, TimeUnit.SECONDS);
    }

    public void decompose(){
        if (driver != null) {
            driver.quit();
        }
    }

    public Browser getBrowser() {
        return browser;
    }

    public WebDriver getDriver() {
        return driver;
    }

    public String getGridHubUrl() {
        return gridHubUrl;
    }

    public String getBaseUrl() {
        return baseUrl;
    }

    public MovieHelper getMovieHelper() {
        return movieHelper;
    }

    public NavigationHelper getNavigationHelper() {
        return navigationHelper;
    }

    public UserHelper getUserHelper() {
        return userHelper;
    }

}
